<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.application.component.model' );
JTable::addIncludePath(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_ecommerce'.DS.'tables');	

class EcommerceModelList extends JModel
{
	var $_catid = 0;
	
	function __construct()
	{
		global $mainframe;
		parent::__construct();
		$catid = intval(JRequest::getVar('catid', 2));
		$this->_catid = $catid;		
	}
	function getList()
	{
		$query = "SELECT id, name FROM #__pr_category WHERE parent_id = ".$this->_catid;			
		$this->_db->setQuery($query);
		$cats = $this->_db->loadObjectList();
		
		$i = 0;
		foreach($cats as $cat){
			$query = "SELECT p.*, com.name as com_name FROM #__pr_product p 
			LEFT JOIN #__pr_product_category pc on p.id = pc.product_id 
			LEFT JOIN #__pr_category c on c.id = pc.category_id 
			LEFT JOIN #__pr_company com on com.id = p.manufacturer  
			WHERE p.published = 1 AND c.id = ".$cat->id;
			$this->_db->setQuery($query);	
			$cats[$i]->products = $this->_db->loadObjectList();
			$i++;
		}
		return $cats;
	}
	
	function getInfo(){
		$query = "SELECT name FROM #__pr_category WHERE id = ".$this->_catid;
		$this->_db->setQuery($query);	
		$info = $this->_db->loadResult();
		return $info;
	}
	
	function getListCategories()
	{
		$query = "SELECT id, alias, name FROM #__pr_category WHERE `published`=1";
		$this->_db->setQuery($query);
		$list = $this->_db->loadObjectList();

		return $list;
	}
	
	function getProductByCatid($catid)
	{
		$lim   = JRequest::getInt('limit',10);
		$lim0  = JRequest::getVar('limitstart', 0, '', 'int');
		$orders = JRequest::getVar('orders', '');
		
		$q = "	SELECT SQL_CALC_FOUND_ROWS p.*
				FROM #__pr_product p join #__pr_product_category ppc on p.id=ppc.product_id
				WHERE category_id = $catid
				";
		if($orders){
			$or = $orders;
		}else{
			$or = "ASC";
		}
		
		$q.= "ORDER BY p.name " . $or;
						
		//echo $q;
		$this->_db->setQuery($q,$lim0,$lim);
		$rL= $this->_db->loadObjectList();
		if (empty($rL)) { return;}
		else {
			$this->_db->setQuery('SELECT FOUND_ROWS();');  //no reloading the query! Just asking for total without limit
			jimport('joomla.html.pagination');
			$pageNav = new JPagination( $this->_db->loadResult(), $lim0, $lim );
			
		}
		//$list = $this->_db->loadObjectList();
		$result["link"] = $pageNav->getPagesLinks(); //Displays a nice footer
		$result["data"] = $rL;
		return $result;
	}
	
	function getProductPaging($catid, $lim, $lim0, $orders)
	{
	
		$q = "	SELECT SQL_CALC_FOUND_ROWS p.*
				FROM #__pr_product p join #__pr_product_category ppc on p.id=ppc.product_id
				WHERE category_id = $catid
				";
		if($orders){
			$or = $orders;
		}else{
			$or = "ASC";
		}
	
		$q.= "ORDER BY p.name " . $or;
		
		$this->_db->setQuery($q,$lim0,$lim);
		$rL= $this->_db->loadObjectList();
		if (empty($rL)) {
			return;
		}else {
			$this->_db->setQuery('SELECT FOUND_ROWS();');  //no reloading the query! Just asking for total without limit
			jimport('joomla.html.pagination');
			$pageNav = new JPagination( $this->_db->loadResult(), $lim0, $lim );
		}
		$result["link"] = $pageNav->getPagesLinks(); //Displays a nice footer
		$result["data"] = $rL;
		return $result;
	}
	
	function getProducttype(){
		$query = "SELECT * FROM #__pr_producttype";
		$this->_db->setQuery($query);
		return $this->_db->loadObjectList();
	}
	
	function getPriceById($category_id, $product_id, $producttype_id="1"){
		$query = "	SELECT price 
					FROM #__pr_productdetail pd JOIN #__pr_product_category pc on pd.product_category_id=pc.id  
					WHERE pc.category_id = $category_id AND pc.product_id=$product_id AND pd.producttype_id=$producttype_id";
				
		$this->_db->setQuery($query);
		return $this->_db->loadResult();
	}
	
	function getProductdetailId($category_id, $product_id, $producttype_id="1"){
		$query = "	SELECT pd.id 
					FROM #__pr_productdetail pd JOIN #__pr_product_category pc on pd.product_category_id=pc.id  
					WHERE pc.category_id = $category_id AND pc.product_id=$product_id AND pd.producttype_id=$producttype_id";
				
		$this->_db->setQuery($query);
		return $this->_db->loadResult();
	}
	
	function getminicart(){
		$cart = array();
		if(!empty($_SESSION["cart"])){
			$cart = $_SESSION["cart"];
		}		
		foreach ($cart as $pro){
			$price = $this->getPriceById($pro["category_id"],$pro["product_id"],$pro["producttype_id"]);
			$totalprice += $price;
		}
		$html  = '<a class="bestil-info" href="mine_bestilinger.html">';
		$html .= 'Antal ydelser : <strong>'.count($cart).'</strong> <br/>';		
		$html .= 'Total beløb : <strong>'.$this->formatPrice($totalprice).' DKK</strong>';
		$html .= '</a>';
		return $html;
	}
	
	function gettotalPrice(){
		$cart = array();
		$totalprice = 0;
		if(!empty($_SESSION["cart"])){
			$cart = $_SESSION["cart"];
		}		
		foreach ($cart as $pro){
			$price = $this->getPriceById($pro["category_id"],$pro["product_id"],$pro["producttype_id"]);
			$totalprice += $price;
		}
		return $this->formatPrice($totalprice);
	}
	
	function gettotalPriceByCatid($category_id=0, $format=1){
		$cart = array();
		$totalprice = 0;
		if(!empty($_SESSION["cart"])){
			$cart = $_SESSION["cart"];
		}		
		foreach ($cart as $pro){
			$price = $this->getPriceById($pro["category_id"],$pro["product_id"],$pro["producttype_id"]);
			if($pro["category_id"] ==  $category_id){
				$totalprice += $price;
			}
		}
		if($format==0){
			return $totalprice;
		}
		return $this->formatPrice($totalprice);
	}
	
	function formatPrice($number){
		$number = number_format($number,2,",","");
		return str_replace(",00", ",-", $number);
	}
	
	function getUserinfo($user_id){
		$q = "SELECT * 
				FROM #__users u join #__users_extension ue on u.id=ue.id
				WHERE ue.id=$user_id";
		$this->_db->setQuery($q);
		return $this->_db->loadObjectList();
	}
}
?>